package com.aleaho.cftrack.utils;

import android.content.Context;
import android.content.res.Resources;

import com.aleaho.cftrack.R;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;

/**
 * @author Aleaho
 * @version 1.0
 * @date 2017/11/15 10:43
 * @desc
 */
public class BitmapUtil {
    public static BitmapDescriptor bmArrowPoint = null;

    public static BitmapDescriptor bmStart = null;

    public static BitmapDescriptor bmEnd = null;

    public static BitmapDescriptor bmGeo = null;

    public static BitmapDescriptor bmGcoding = null;

    public static BitmapDescriptor bmAccel = null;
    public static BitmapDescriptor bmStay = null;
    public static BitmapDescriptor bmSpeeding = null;
    public static BitmapDescriptor bmSteering = null;
    public static BitmapDescriptor bmBreaking = null;

    /**
     * 创建bitmap，在MainActivity onCreate()中调用
     */
    public static void init() {
        bmArrowPoint = BitmapDescriptorFactory.fromResource(R.drawable.icon_point);
        bmStart = BitmapDescriptorFactory.fromResource(R.drawable.icon_start);
        bmEnd = BitmapDescriptorFactory.fromResource(R.drawable.icon_end);
        bmGeo = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo);
        bmGcoding = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);
        bmAccel = BitmapDescriptorFactory.fromResource(R.drawable.icon_analysis_accel);
        bmStay = BitmapDescriptorFactory.fromResource(R.drawable.icon_analysis_stay);
        bmSpeeding = BitmapDescriptorFactory.fromResource(R.drawable.icon_analysis_speeding);
        bmSteering = BitmapDescriptorFactory.fromResource(R.drawable.icon_analysis_steering);
        bmBreaking = BitmapDescriptorFactory.fromResource(R.drawable.icon_analysis_breaking);
    }

    /**
     * 回收bitmap，在MainActivity onDestroy()中调用
     */
    public static void clear() {
        bmArrowPoint.recycle();
        bmStart.recycle();
        bmEnd.recycle();
        bmGeo.recycle();
        bmGcoding.recycle();
        bmAccel.recycle();
        bmStay.recycle();
        bmSpeeding.recycle();
        bmSteering.recycle();
        bmBreaking.recycle();
    }

    public static BitmapDescriptor getMark(Context context, int index) {
        Resources res = context.getResources();
        int resourceId;
        if (index <= TrackConsts.MAX_MARKER_COUNT) {
            resourceId = res.getIdentifier("icon_mark" + index, "mipmap", context.getPackageName());
        } else {
            resourceId = res.getIdentifier("icon_markx", "mipmap", context.getPackageName());
        }
        return BitmapDescriptorFactory.fromResource(resourceId);
    }
}
